//
//  ZXZMainViewController.swift
//  up_learnWB
//
//  Created by Mac on 16/10/20.
//  Copyright © 2016年 www.ZXZ123.com. All rights reserved.
//

import UIKit

class ZXZMainViewController: UITabBarController {

    private lazy var composeBtn:UIButton =  UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")

    @objc private func composeBtnClick(){
        print("composeBtnClick")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setupChildViewController()

        setupComposeBtn()

    }





    private  func setupComposeBtn() {
        let length:CGFloat = self.view.width/5-1;


        tabBar.addSubview(composeBtn)

        composeBtn.frame = tabBar.bounds.insetBy(dx: (length)*2, dy: 0)

        composeBtn.addTarget(self, action: #selector(ZXZMainViewController.composeBtnClick), for: UIControlEvents.touchUpInside)

    }
}

// MARK: - extension 类似于OC的分类. 在swift中可以用来切分代码块
// MARK: - 可以吧相近功能的函数, 放在一个extension中 便于代码的维护
// MARK: - z注意: 与OC的分类一样, extension不能定义属性
extension ZXZMainViewController{


    /// 设置所有的子控制器
    func setupChildViewController(){


        // 0. 获取沙盒 json 路径
        let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let jsonPath = (docDir as NSString).appendingPathComponent("main.json")

        // 加载 data
        var data = NSData(contentsOfFile: jsonPath)

        // 判断 data 是否有内容，如果没有，说明本地沙盒没有文件
        if data == nil {
            // 从 Bundle 加载 data
            let path = Bundle.main.path(forResource: "main.json", ofType: nil)

            data = NSData(contentsOfFile: path!)
        }

        // data 一定会有一个内容，反序列化
        // 反序列化转换成数组
        guard let array = try? JSONSerialization.jsonObject(with: data! as Data, options: []) as? [[String: AnyObject]]
            else {
                return
        }



        var addArray = [UIViewController]()

        for dic in array! {
            addArray.append(childViewController(dict: dic ))
        }
        viewControllers = addArray


    }


    private func childViewController(dict:[String:AnyObject]) -> UIViewController{

        guard   let clsName = dict["clsName"],
            let titleName = dict["titleName"],
            let imgName  = dict["imgName"],
            // 反射 弄成控制器
            let cls = NSClassFromString(Bundle.main.namespace+"."+(clsName as! String)) as? ZXZBaseViewController.Type,
            let dic:[String:String] = dict["visitInfo"] as! [String : String]?
            else {
                return  UIViewController()
        }

        // 创建视图控制器

        let vc = cls.init()

        vc.title = titleName as? String
        vc.tabBarItem.image = UIImage(named: "tabbar_"+(imgName as! String))?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_"+(imgName as! String)+"_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)

        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.black, NSFontAttributeName:UIFont.systemFont(ofSize: 13)], for: UIControlState.normal)

        vc.tabBarItem .setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.cyan, NSFontAttributeName:UIFont.systemFont(ofSize: 13)], for: UIControlState.selected)
        vc.inputVisitDic = dic
        let nav = ZXZNavViewController(rootViewController: vc)
        
        return nav
    }
    
    
}

